package com.smartleanx.module.hrm.service.esign.auth.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartleanx.framework.tenant.core.aop.TenantIgnore;
import com.smartleanx.module.hrm.dal.dataobject.esign.ElecCorp;
import com.smartleanx.module.hrm.dal.dataobject.esign.EsignOrgSeal;
import com.smartleanx.module.hrm.dal.mysql.esign.ElecCorpMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.EsignOrgSealMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.EsignSealContractInfoMapper;
import com.smartleanx.module.hrm.service.esign.EsignSealContractInfo;
import com.smartleanx.module.hrm.service.esign.auth.ElecSealService;
import com.smartleanx.module.hrm.service.esign.vo.OrgSealSelectVo;
import com.smartleanx.module.hrm.service.examine.util.exception.ResultException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.security.PermitAll;
import java.util.List;

@Service
public class ElecSealServiceImpl implements ElecSealService {

    @Autowired
    private ElecCorpMapper elecCorpMapper;
    @Autowired
    private EsignOrgSealMapper esignOrgSealMapper;
    @Autowired
    private EsignSealContractInfoMapper esignSealContractInfoMapper;

    @Override
    @TenantIgnore
    public OrgSealSelectVo selectSealInfo(Integer corpId) {
        ElecCorp elecCorp = elecCorpMapper.selectById(corpId);
        if (ObjectUtils.isEmpty(elecCorp) || elecCorp.getEsignAuthorizedStatus() == 0) {
            throw new ResultException("企业不存在或未认证，请刷新后重试");
        }
        EsignOrgSeal esignOrgSeal = esignOrgSealMapper.selectOne(new LambdaQueryWrapper<EsignOrgSeal>() {{
            eq(EsignOrgSeal::getOrgId, elecCorp.getEsignOrgId());
        }});
        OrgSealSelectVo orgSealSelectVo = new OrgSealSelectVo();
        BeanUtils.copyProperties(esignOrgSeal, orgSealSelectVo);
        orgSealSelectVo.setCorpName(elecCorp.getCorpName());
        return orgSealSelectVo;
    }

    @Override
    @TenantIgnore
    @PermitAll
    public PageInfo<EsignSealContractInfo> selectPage(Integer pageNum, Integer pageSize, String sealId, String signTImeStart, String signTimeEnd, String signName) {
        PageHelper.startPage(pageNum, pageSize);
        System.err.println("开始查询印章业务");
        List<EsignSealContractInfo> esignSealContractInfoList = esignSealContractInfoMapper.selectList(new LambdaQueryWrapper<EsignSealContractInfo>() {{
            eq(EsignSealContractInfo::getSealId, sealId);
            ge(!ObjectUtils.isEmpty(signTImeStart), EsignSealContractInfo::getESignTime, signTImeStart);
            le(!ObjectUtils.isEmpty(signTimeEnd), EsignSealContractInfo::getESignTime, signTimeEnd);
            like(!ObjectUtils.isEmpty(signName), EsignSealContractInfo::getEEmployeeName, signName);
            orderByDesc(EsignSealContractInfo::getId);
        }});


        return new PageInfo<>(esignSealContractInfoList);
    }
}
